package com.zxp.springboot.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import vip.xiaonuo.core.consts.CommonConstant;

/**
 * swagger配置
 * 加入分组功能(默认注释掉)  修改packagedToMatch即可
 * <p>
 * https://doc.xiaominfo.com/knife4j/changelog/2017-12-18-swagger-bootstrap-ui-1.7-issue.html
 * </p>
 * @author shollin
 * @date 2024/9/9 10:42
 **/
@Slf4j
@Configuration
@ConditionalOnProperty(name = "knife4j.enable",havingValue = "true")
public class Knife4jConfig {

    //    http://localhost:8282/doc.html        访问Knife4j的文档地址：http://ip:port/doc.html
    /**
     * 根据@Tag 上的排序，写入x-order
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            // 全局添加鉴权参数
            if(openApi.getPaths()!=null){
                openApi.getPaths().forEach((s, pathItem) -> {
                    pathItem.readOperations().forEach(operation -> {
                        operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
                    });
                });
            }
        };
    }

    /**
     * 配置分组，不在这里配的话，可以在yaml文件中配
     *  springdoc.group-configs[0].group=browser
     springdoc.group-configs[0].packages-to-scan=com.zxpnet.browser.controller
     springdoc.group-configs[0].paths-to-match==/**
     * @return
     */
    @Bean
    public GroupedOpenApi userApi(){
        String[] paths = { "/**" };
        String[] packagedToMatch = { "com.zxp" };
        return GroupedOpenApi.builder().group("browser")
                .pathsToMatch(paths)
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public OpenAPI customOpenAPI() {
        log.info("开始实例化SwaggerConfig");
        return new OpenAPI()
                .info(new Info()
                        .title("ZXPNET APIs")
                        .version("1.0")
                        .description( "©2024 Copyright. Powered By shollin.更多请关注：http://www.zxpnet.com")
                        .termsOfService("http://www.zxpnet.com")
                        .license(new License().name("Apache 2.0")
                                .url("http://doc.xiaominfo.com"))
                )
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION,new SecurityScheme()
                        .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP).scheme("bearer")));
    }

}
